var express = require('express');
var session = require('express-session');
var path = require('path');
var router = express.Router();
var sqlite3 = require('sqlite3').verbose();

/* GET users listing. */
router.get('/', function (req, res, next) {
  var redata = [];
  var db = new sqlite3.Database(path.join(__dirname, "../db/data.s3db"));
  var n1 = 1,
    n2 = 1,
    n3 = 1;
  while (n1 == 1 || n1 == 3 || n1 == 4 || n1 == req.query.mid) {
    n1 = Math.floor(Math.random() * 12 + 1);
  }
  while (n2 == 1 || n2 == 3 || n2 == 4 || n1 == req.query.mid || n2 == n1) {
    n2 = Math.floor(Math.random() * 12 + 1);
  }
  while (n3 == 1 || n3 == 3 || n3 == 4 || n3 == req.query.mid || n3 == n1 || n3 == n2) {
    n3 = Math.floor(Math.random() * 12 + 1);
  }
  console.log(n1 + " " + n2 + " " + n3);
  try {
    db.serialize(function () {
      sqlStr = "SELECT * from material where mid=='" + req.query.mid + "' or mid=='" + n1 + "' or mid=='" + n2 + "' or mid=='" + n3 + "'";
      db.all(sqlStr, function (err, rows) {
        redata = rows;
        console.log(redata);
        res.send(redata);
      });
    });
  } catch (ex) {
    console.log(ex);
  }
});

module.exports = router;